<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<title>基本设置</title>
	<link rel="stylesheet" href="/js/plugin/layui-v2.3.0/layui/css/layui.css">
	<link rel="stylesheet" href="/css/reset.css">
	<link rel="stylesheet" href="/css/index.css">
	<script src="/js/jquery-2.1.1.min.js"></script>
	<script src="/js/plugin/layui-v2.3.0/layui/layui.js"></script>
	
</head>
<style>
	body,html{
		height: auto; 
	}
</style>
<body>
	<input id="projectId" name="projectId" type="hidden" th:value="${projectId}" />
    <div class="nav-section-content-container" style="padding: 0px">
		<header class="nav-header">
			<div class="nav-section-header-title">
				<span>楼层管理</span>
				<div class='nav-header-btn f-right' id="add-btn"><span class="layui-btn layui-btn-radius layui-btn-normal">添加大楼</span></div>
			</div>
		</header>
		<div class="nav-section-content-container" id="buildingList">
			
			
		</div>
		<script id="Bar" type="text/html">
	  		<div class="">
				<a  href="javascript:void(0)" onclick="editFloor('{{d.id}}','{{d.fkBuilding}}')"><i class="layui-icon icon-settings">&#xe642;</i></a>&nbsp;
				<a  href="javascript:void(0)" onclick="delFloor('{{d.id}}','{{d.fkBuilding}}')"><i class="layui-icon icon-settings">&#xe640;</i></a>
			</div>
		</script>
</body>
<script>
var projectId=$("#projectId").val();
var table;
	$(function(){
		
		layui.use(['element',"layer","form","table"], function(){
			var layer=layui.layer,form = layui.form;
			table = layui.table;
			//setHeight();
			$("#add-btn").on("click",function(){
				parent.parent.parent.layer.open({
				  type: 2, 
				  title:"添加大楼",
				  area: ['700px','200px'],
			      shade: [0.6, '#010101'],
			      zIndex:800,
				  content:"/app/projectBuilding/add/"+projectId , //这里content是一个普通的String
				  end: function () {
					  runAsync();
			      }
				});		
			})
			runAsync();
			
		});

	})
	//params:{keyword: keyword}
	//封装获取数据
	function runAsync(url){
		
	　　var def = $.Deferred();
	　	var url=url || "/app/projectBuilding/listdata";
		jQuery.ajax({
	        type:"post",
	        async:false,
	        url:url,
	        dataType:"json",
	        data:{'params[projectId]':$("#projectId").val()},
	        success:function(data){
	        	resolve(data.data);
	        }
	    })
	}
	function resolve(data){
		$("#buildingList").html('');
		
		for (var i = 0; i < data.length; i++) {
			var cont='<div class="pad-top-30"><div class=" clearfix border-bottom pad-bot-20"><div class="f-left">'
			+'<span class="font-18 ">'+data[i].buildingName+'</span></div><div class="f-right">'
			+'<button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal " id="'+data[i].id+'" onclick="addFloor(this)">添加楼层</button>'
			+'<button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal " id="'+data[i].id+'" onclick="editBuilding(this)">修改大楼</button>'
			+'<button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal " id="'+data[i].id+'" onclick="delBuilding(this)">删除大楼</button>'
			+'</div></div><div><table class="layui-hide" id="LAY_table_building'+data[i].id+'" lay-filter="'+data[i].id+'"></table></div></div>';
			$("#buildingList").append(cont);
			chuTable("#LAY_table_building"+data[i].id,data[i].id);
			
		}
		
	}
	function addFloor(data){
		parent.parent.parent.layer.open({
			  type: 2, 
			  title:"添加楼层",
			  area: ['700px','350px'],
		      shade: [0.6, '#010101'],
		      zIndex:800,
			  content:"/app/projectFloor/add/"+projectId+"_"+data.id , //这里content是一个普通的String
			  end: function () {
				  layui.table.reload(data.id, {
				        page: {
				          curr: 1 //重新从第 1 页开始
				        }
				        ,where: {
				        	params:{
				        		buildingId: data.id
				        	}
				        }
				    });
		      }
			});
	}
	function editBuilding(data){
		parent.parent.parent.layer.open({
		  type: 2, 
		  title:"修改大楼",
		  area: ['700px','200px'],
	      shade: [0.6, '#010101'],
	      zIndex:800,
		  content:"/app/projectBuilding/edit/"+data.id , //这里content是一个普通的String
		  end: function () {
			  runAsync();
	      }
		});		
	}
	function delBuilding(data){
		parent.parent.parent.layer.confirm('确认要删除吗，删除后不能恢复', function(index){
	    	//发送删除请求
	    	$.post("/app/projectBuilding/delete",{id:data.id},function(res){
	    		if(res.success){
	    			parent.parent.parent.layer.msg(res.msg);
	    		}else{
	    			parent.parent.parent.layer.msg(res.msg);
	    		}
	    		parent.parent.parent.layer.close(index);
	    		runAsync();
	        },"json");
	  	});
	}
	function editFloor(id,buildingId){
		parent.parent.parent.layer.open({
			  type: 2, 
			  title:"修改楼层",
			  area: ['700px','350px'],
		      shade: [0.6, '#010101'],
		      zIndex:800,
			  content:"/app/projectFloor/edit/"+id , //这里content是一个普通的String
			  end: function () {
				  layui.table.reload(buildingId, {
			          page: {
			            curr: 1 //重新从第 1 页开始
			          }
			          ,where: {
			          	  params:{
			        		  buildingId: buildingId
			        	  }
			          }
				  });
		      }
			});
	}
	function delFloor(id,buildingId){
		parent.parent.parent.layer.confirm('确认要删除吗，删除后不能恢复', function(index){
	    	//发送删除请求
	    	$.post("/app/projectFloor/delete",{id:id},function(res){
	    		if(res.success){
	    			parent.parent.parent.layer.msg(res.msg);
	    		}else{
	    			parent.parent.parent.layer.msg(res.msg);
	    		}
	    		parent.parent.parent.layer.close(index);
	    		layui.table.reload(buildingId, {
			        page: {
			          curr: 1 //重新从第 1 页开始
			        }
			        ,where: {
			        	params:{
			        		buildingId: buildingId
			        	}
			        }
			    });
	        },"json");
	  	});
	}
	function chuTable(id,eame){
		//方法级渲染
		table.render({
			id:eame,
		    elem: id,
		    url: '/app/projectFloor/listdata',
		    cols: [[
		      {field:'describe', align:'left', title: '大楼信息',width:'30%'}
		      ,{field:'name', align:'left', title: '第几层',width:'20%'}
		      ,{field:'remark', align:'left', title: '备注',width:'30%'}
		      ,{field:'Bar',toolbar: '#Bar', align:'center', title: '操作',width:'16%'}
		    ]],
		    limit:3,
		    page:true,
		    where: {
	        	params:{
	        		buildingId: eame
	        	}
	        },
	        done:function(){
	        	setHeight();
	        }
	        
		});
	}

	function setHeight(){
        var h = document.body.clientHeight;
        //consol.log(h,0)
        window.parent.setiframeWrap(h);
    }
</script>
</html>